#!/bin/bash
#
# This script captures test code coverage information.

set -e

function install_llvm_gcov() {
    cat << EOF > /tmp/llvm-gcov.sh
#!/bin/bash
exec /usr/bin/llvm-cov gcov "\$@"
EOF
    sudo chmod 755 /tmp/llvm-gcov.sh
}

if [[ `uname` == "Darwin" ]]; then
    echo "gcov is llvm-cov on macOS"
    lcov --capture --directory . --output-file coverage.info
else
    echo "call llvm-cov on Linux"
    install_llvm_gcov
    lcov --gcov-tool /tmp/llvm-gcov.sh --capture --directory . --output-file coverage.info
fi

lcov --remove coverage.info '/usr/*' --output-file coverage.info
lcov --remove coverage.info '/Applications/*' --output-file coverage.info
lcov --remove coverage.info '*/build/*' --output-file coverage.info
lcov --remove coverage.info '*.pb.cc' --output-file coverage.info
lcov --remove coverage.info '*.pb.h' --output-file coverage.info
lcov --remove coverage.info '*/tests/*' --output-file coverage.info
lcov --remove coverage.info '*/samples/*' --output-file coverage.info
bash <(curl -s https://codecov.io/bash) -f coverage.info -t 1faef852-b196-4697-885b-0ff22a625181 || echo "Codecov did not collect coverage reports"

# Generate HTML report
if [[ "$1" == "html" ]]; then
    genhtml --output-directory coverage \
        --demangle-cpp --num-spaces 4 --sort \
        --title "Trust Wallet Core Test Coverage" \
        --function-coverage --branch-coverage --legend \
        coverage.info
fi
